package pl.ydp.ivy.flex.ide;

import java.io.PrintWriter;

import org.apache.ivy.util.AbstractMessageLogger;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.MessageConsole;

public class ConsoleMessageLogger extends AbstractMessageLogger {

    private int level;
    PrintWriter consoleOutput;

    public ConsoleMessageLogger(int level)
    {
        this.level = level;
        consoleOutput = new PrintWriter( getConsole().newOutputStream() );
    }

    public int getLevel()
    {
        return level;
    }
	@Override
	protected void doEndProgress(String s) {
		consoleOutput.println(s);
	}

	@Override
	protected void doProgress() {
		consoleOutput.print(".");
	}

	public void log(String s, int i) {
        if(level <= this.level) {
        	consoleOutput.println(s);
        	consoleOutput.flush();
        }
	}

	public void rawlog(String s, int i) {
		log( s, i );
	}

	static IOConsole console;
	public static IOConsole getConsole() {
		if ( console==null ) {
			console = new MessageConsole( "Ivy/Flex", Activator.getImageDescriptor( "icon/resolve-all.png" ) );
			ConsolePlugin.getDefault().getConsoleManager().addConsoles( new IConsole[]{ console } );
			ConsolePlugin.getDefault().getConsoleManager().showConsoleView( console );
		}
		
		return console;
	}
}
